﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.IO;

using EHTWebsite.Code;
/// <summary>
/// Author: toanvt
/// CreatedDate: 12/12/2011
/// Description: Danh sách ảnh sản phẩm
/// </summary>
public partial class Administrator_AdminProductImages : System.Web.UI.Page
{
    public Int32 sanPhamID = HangSo.DEFAULT_ID;
    private Int32 pageIndex = 1;
    private Int32 pageSize = 10;
    private Int32 totalRecord = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            sanPhamID = Int32.Parse(Request.QueryString["SanPhamID"].ToString());
        }
        catch (Exception)
        {
            sanPhamID = HangSo.DEFAULT_ID;
        }

        try
        {
            pageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
        }
        catch (Exception)
        {
            pageIndex = 1;
        }

        if (!Page.IsPostBack)
        {
            LoadDataAnhSanPhams(sanPhamID);
        }
    }

    /// <summary>
    /// Author: toanvt
    /// CreatedDate: 12/12/2011
    /// Description: Lấy danh sách ảnh sản phẩm
    /// </summary>
    /// <param name="sanPhamID">Mã sản phẩm</param>
    private void LoadDataAnhSanPhams(Int32 sanPhamID)
    {
        rpProductImages.DataSource = HinhAnhSanPhamProvider.GetHinhAnhSanPhamsBySanPhamIDForFrontEnd(sanPhamID, pageIndex, pageSize, out totalRecord);
        rpProductImages.DataBind();

        foreach (RepeaterItem item in rpProductImages.Items)
        {
            CheckBox ckPublish = (CheckBox)item.FindControl("ckPublish");

            //Hiện biểu tượng tương ứng với trạng thái Publish
            if (ckPublish.Checked == true)
            {
                ((ImageButton)item.FindControl("btnPublish")).ImageUrl = "Templates/Images/btncheck.png";
            }
            else
            {
                ((ImageButton)item.FindControl("btnPublish")).ImageUrl = "Templates/Images/unpublish.jpg";
            }
        }

        PaggingControl1.PageURL = "AdminProductImages.aspx?SanPhamID=" + sanPhamID.ToString();
        PaggingControl1.PageIndex = pageIndex;
        PaggingControl1.TotalRecord = totalRecord;
    }

    protected void rpProductImages_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        String cmdName = e.CommandName;
        Int32 hinhAnhID = -1;

        try
        {
            hinhAnhID = Int32.Parse(e.CommandArgument.ToString());
        }
        catch (Exception ex)
        {
        }

        if (cmdName == "delete1")
        {
            HinhAnhSanPhamProvider.DeleteHinhAnhSanPhamByHinhAnhID(hinhAnhID);
            LoadDataAnhSanPhams(sanPhamID);
        }
        else if (cmdName == "publish")
        {
            HinhAnhSanPhamProvider.ChangePublishByHinhAnhSanPhamID(hinhAnhID);
            LoadDataAnhSanPhams(sanPhamID);
        }
        else if (cmdName == "change_number_of_order")
        {
            try
            {
                Int32 numberOfOrder = Int32.Parse(((TextBox)e.Item.FindControl("txtNumberOfOrder")).Text.Trim());
                HinhAnhSanPhamProvider.UpdateNumberOfOrderByHinhAnhSanPhamID(hinhAnhID, numberOfOrder);
            }
            catch (Exception ex)
            {

            }

            LoadDataAnhSanPhams(sanPhamID);
        }
        else if (cmdName == "save_all")
        {
            foreach (RepeaterItem item in rpProductImages.Items)
            {
                Int32 numberOfOrder = Int32.Parse(((TextBox)item.FindControl("txtNumberOfOrder")).Text.Trim());
                hinhAnhID = Int32.Parse(((Literal)item.FindControl("ltrHinhAnhID")).Text.Trim());
                HoTroProvider.UpdateNumberOfOrderByHoTroID(hinhAnhID, numberOfOrder);
            }

            LoadDataAnhSanPhams(sanPhamID);
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Administrator/AdminProductImageIU.aspx?SanPhamID=" +sanPhamID.ToString());
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Administrator/AdminProductIU.aspx?SanPhamID=" + sanPhamID.ToString());
    }
}
